package com.edfu.core.controller;

import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.edfu.core.institutemgt.service.api.IInstituteMgtService;
import com.edfu.core.institutemgt.vo.AcademicSemester;
import com.edfu.core.institutemgt.vo.Batch;
import com.edfu.core.institutemgt.vo.BatchSemesterAssn;
import com.edfu.core.institutemgt.vo.Branch;
import com.edfu.core.institutemgt.vo.Institute;
import com.edfu.core.util.EduServiceFactory;
import com.edfu.core.util.Response;

public class InstituteMgtController implements IInstituteMgtService {
	
	@Override
	@RequestMapping(value="/institute", method=RequestMethod.POST)
	public @ResponseBody Response addInstitute(@RequestBody Institute institute) {
		Response r = null ;
		try {
			r = EduServiceFactory.getInstituteMgtService().addInstitute(institute);
		}catch ( Exception e ) {
			r = _getFailureResponse(e);
		}
		return r;
	}

	@Override
	public @ResponseBody Response updateInstitute(Institute institute) {
		Response r = null ;
		try {
			r = EduServiceFactory.getInstituteMgtService().updateInstitute(institute);
		}catch ( Exception e ) {
			r = _getFailureResponse(e);
		}
		return r;
	}

	@Override
	public @ResponseBody Response deleteInstitute(String instituteCode) {
		Response r = null ;
		try {
			r =  EduServiceFactory.getInstituteMgtService().deleteInstitute(instituteCode);
		}catch ( Exception e ) {
			r = _getFailureResponse(e);
		}
		return r;
	}

	@Override
	public Institute getInstituteDetails(String instituteCode) {
		Institute i = null ;
		try {
			i =  EduServiceFactory.getInstituteMgtService().getInstituteDetails(instituteCode);
		}catch ( Exception e ) {
			throw new RuntimeException(e);
		}
		return i;
	}

	@Override
	@RequestMapping(value="/branch", method=RequestMethod.POST)
	public @ResponseBody Response addBranch(@RequestBody Branch branch) {
		Response r = null ;
		try {
			r = EduServiceFactory.getInstituteMgtService().addBranch(branch);
		}catch ( Exception e ) {
			r = _getFailureResponse(e);
		}
		return r;
	}

	@Override
	public @ResponseBody Response updateBranch(Branch branch) {
		Response r = null ;
		try {
			r =  EduServiceFactory.getInstituteMgtService().updateBranch(branch);
		}catch ( Exception e ) {
			r = _getFailureResponse(e);
		}
		return r;
	}

	@Override
	public @ResponseBody Response deleteBranch(String branchCode) {
		Response r = null ;
		try {
			r = EduServiceFactory.getInstituteMgtService().deleteBranch(branchCode);
		}catch ( Exception e ) {
			r = _getFailureResponse(e);
		}
		return r;
	}
	
	@Override
	public @ResponseBody Response addBatch(Batch batch) {
		Response r = null ;
		try {
			r = EduServiceFactory.getInstituteMgtService().addBatch(batch);
		}catch ( Exception e ) {
			r = _getFailureResponse(e);
		}
		return r;
	}

	@Override
	public @ResponseBody Response updateBatch(Batch batch) {
		Response r = null ;
		try {
			r = EduServiceFactory.getInstituteMgtService().addBatch(batch);
		}catch ( Exception e ) {
			r = _getFailureResponse(e);
		}
		return r;
	}

	@Override
	public @ResponseBody Response deleteBatch(String batchCode) {
		Response r = null ;
		try {
			r = EduServiceFactory.getInstituteMgtService().deleteBatch(batchCode);
		}catch ( Exception e ) {
			r = _getFailureResponse(e);
		}
		return r;
	}
	
	@Override
	public @ResponseBody Response associateSemestersToBranch(List<String> semesterCodes,
			String branchCode) {
		Response r = null ;
		try {
			r = EduServiceFactory.getInstituteMgtService().associateSemestersToBranch(semesterCodes, branchCode);
		}catch ( Exception e ) {
			r = _getFailureResponse(e);
		}
		return r;
	}

	@Override
	public @ResponseBody Response associateSemesterToBranch(String semesterCode, String branchCode) {
		Response r = null ;
		try {
			r = EduServiceFactory.getInstituteMgtService().associateSemesterToBranch(semesterCode, branchCode);
		}catch ( Exception e ) {
			r = _getFailureResponse(e);
		}
		return r;
	}

	@Override
	public @ResponseBody Response assignBatchToSemester(BatchSemesterAssn batchSemAssn) {
		Response r = null ;
		try {
			r = EduServiceFactory.getInstituteMgtService().assignBatchToSemester(batchSemAssn);
		}catch ( Exception e ) {
			r = _getFailureResponse(e);
		}
		return r;
	}
	
	@Override
	public @ResponseBody Response editBatchSemesterAssociation(String batchCode,
			String semesterCode, Date startsOn, Date endsOn) {
		Response r = null ;
		try {
			r = EduServiceFactory.getInstituteMgtService().editBatchSemesterAssociation(batchCode, semesterCode, startsOn, endsOn);
		}catch ( Exception e ) {
			r = _getFailureResponse(e);
		}
		return r;
	}
	
	@Override
	public @ResponseBody Response addSemester(AcademicSemester semester) {
		Response r = null ;
		try {
			r = EduServiceFactory.getInstituteMgtService().addSemester(semester);
		}catch ( Exception e ) {
			r = _getFailureResponse(e);
		}
		return r;
	}
	
	private Response _getFailureResponse(Exception e) {
		Response response = new Response();
		response.setSuccessful(false);
		response.setResponseCode("Internal Application Error.");
		response.setResponseMessage(e.getMessage());
		return response;
	}

}
